﻿@namespace MudBlazor
@inherits MudComponentBase

<div @attributes="UserAttributes" class="@TabsClassnames" style="@Style">
    <CascadingValue Value="this" IsFixed="true">
        <div class="@TabBarClassnames">
            <div class="mud-tabs-tabbar-inner" style="@MaxHeightStyles">
                @if (HeaderPosition == TabHeaderPosition.Before && Header != null)
                {
                    <div class="mud-tabs-header mud-tabs-header-before">
                        @Header(this)
                    </div>
                }
                @if (_showScrollButtons)
                {
                    <div class="mud-tabs-scroll-button">
                        <MudIconButton Icon="@_prevIcon" Color="@ScrollIconColor" OnClick="@((e) => ScrollPrev())" Disabled="@_prevButtonDisabled" />
                    </div>
                }
                <div @ref="@_tabsContentSize" class="mud-tabs-tabbar-content">
                    <div class="@WrapperClassnames" style="@WrapperScrollStyle">
                        @foreach (MudTabPanel panel in _panels)
                        {
                            @if (panel.TabContent == null && panel.TabWrapperContent == null)
                            {
                                <MudTooltip Placement="@GetTooltipPlacement()" Text="@panel.ToolTip" @key="@panel">
                                    @RenderTab(panel)
                                </MudTooltip>
                            }
                            else
                            {
                                <div class="d-inline-block" style="width: fit-content;" @key="@panel">
                                    @if (panel.TabWrapperContent is null) 
                                    {
                                        @RenderTab(panel)
                                    }
                                    else
                                    {
                                        @panel.TabWrapperContent(RenderTab(panel))
                                    }
                                </div>
                            }
                        }
                        @if (!HideSlider && IsSliderPositionDetermined)
                        {
                            <div class="@SliderClass" style="@SliderStyle"></div>
                        }
                    </div>
                </div>
                @if (_showScrollButtons)
                {
                    <div class="mud-tabs-scroll-button">
                        <MudIconButton Icon="@_nextIcon" Color="@ScrollIconColor" OnClick="@((e) => ScrollNext())" Disabled="@_nextButtonDisabled" />
                    </div>
                }
                @if (HeaderPosition == TabHeaderPosition.After && Header != null)
                {
                    <div class="mud-tabs-header mud-tabs-header-after">
                        @Header(this)
                    </div>
                }
            </div>
        </div>
        @if (PrePanelContent != null)
        {
            @PrePanelContent(ActivePanel)
        }
        <div class="@PanelsClassnames">
            @ChildContent
        </div>
    </CascadingValue>
</div>

@code {
    RenderFragment RenderTab(MudTabPanel panel) => @<div @ref="panel.PanelRef" class="@GetTabClass(panel)" style="@GetTabStyle(panel)" @onclick=@( e => ActivatePanel(panel, e, false) )>
        @if (TabPanelHeaderPosition == TabHeaderPosition.Before && TabPanelHeader != null)
            {
                <div class="mud-tabs-panel-header mud-tabs-panel-header-before">
                    @TabPanelHeader(panel)
                </div>
            }
            @if (panel.TabContent != null)
            {
                @panel.TabContent
            }
            else if (!String.IsNullOrEmpty(panel.Text) && String.IsNullOrEmpty(panel.Icon))
            {
                @panel.Text
            }
            else if (String.IsNullOrEmpty(panel.Text) && !String.IsNullOrEmpty(panel.Icon))
            {
                <MudIcon Icon="@panel.Icon" Color="@GetPanelIconColor(panel)" />
            }
            else if (!String.IsNullOrEmpty(panel.Text) && !String.IsNullOrEmpty(panel.Icon))
            {
                <MudIcon Icon="@panel.Icon" Color="@GetPanelIconColor(panel)" Class="mud-tab-icon-text" />
                @panel.Text
            }
            @if (panel.BadgeData != null || panel.BadgeDot)
            {
                <MudBadge Dot="@panel.BadgeDot" Content="@panel.BadgeData" Color="@panel.BadgeColor" Class="mud-tab-badge" />
            }
            @if (TabPanelHeaderPosition == TabHeaderPosition.After && TabPanelHeader != null)
            {
                <div class="mud-tabs-panel-header mud-tabs-panel-header-after">
                    @TabPanelHeader(panel)
                </div>
            }
        </div>;
}